class Solution {
public:
    int findSubstringInWraproundString(string s) {
        const int m = s.size();
        vector<int> dp(m + 1, 1);
        int hash[26];

        for(int i = 1; i < m; ++i)
        {
            if(s[i - 1] == s[i] - 1 || s[i] == 'a' && s[i - 1] == 'z')
                dp[i] = dp[i - 1] + 1;
        }

        for(int i = 1; i <= m; ++i)
            hash[s[i - 1] - 'a'] = max(hash[s[i - 1] - 'a'], dp[i]);

        int ret = 0;
        for(auto e : hash)
            ret += e;
        return ret;
    }
};
